<?php
/**
 * 获取系统所有功能菜单和操作按钮信息
 * 通过查询oa_admin_rule表获取完整的权限规则
 */

// 引入自动加载文件
require_once 'vendor/autoload.php';

// 数据库配置
$config = [
    'hostname' => '116.62.146.217',
    'database' => 'oa',
    'username' => 'root',
    'password' => 'JyDmXg0407',
    'hostport' => '3306',
    'prefix'   => 'oa_',
    'charset'  => 'utf8mb4',
];

try {
    // 创建数据库连接
    $dsn = "mysql:host={$config['hostname']};port={$config['hostport']};dbname={$config['database']};charset={$config['charset']}";
    $pdo = new PDO($dsn, $config['username'], $config['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);

    // 查询oa_admin_rule表
    $stmt = $pdo->query("SELECT * FROM oa_admin_rule ORDER BY pid, id");
    $rules = $stmt->fetchAll();

    echo "========================================\n";
    echo "系统功能菜单和操作按钮信息\n";
    echo "========================================\n";
    echo "总共找到 " . count($rules) . " 条规则\n\n";

    // 按层级显示规则
    displayRules($rules);

    // 保存到JSON文件以便后续分析
    file_put_contents('system_rules.json', json_encode($rules, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    echo "\n规则已保存到 system_rules.json 文件中\n";

} catch (PDOException $e) {
    echo "数据库连接错误: " . $e->getMessage() . "\n";
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

/**
 * 按层级显示规则
 */
function displayRules($rules, $parentId = 0, $level = 0) {
    $indent = str_repeat("  ", $level);
    
    foreach ($rules as $rule) {
        if ($rule['pid'] == $parentId) {
            $status = $rule['status'] == 1 ? '启用' : '禁用';
            echo "{$indent}[ID:{$rule['id']}] {$rule['title']} ({$rule['name']}) - {$status}\n";
            
            // 显示详细信息（仅对非顶级菜单）
            if ($level > 0 && !empty($rule['condition'])) {
                echo "{$indent}  条件: {$rule['condition']}\n";
            }
            
            // 递归显示子规则
            displayRules($rules, $rule['id'], $level + 1);
        }
    }
}